package com.leetcode.p0;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 回溯
 */
public class Test17_2 {

    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<>();
        if (digits.length() == 0) {
            return combinations;
        }

        Map<Character, String> phoneMap = new HashMap<>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};

        helper(combinations, phoneMap, digits, 0, new StringBuilder());

        return combinations;
    }

    private void helper(List<String> combinations, Map<Character, String> phoneMap, String digits, int currentIndex, StringBuilder stringBuilder) {
        if (digits.length() == currentIndex) {
            combinations.add(stringBuilder.toString());
            return;
        }

        String currentString = phoneMap.get(digits.charAt(currentIndex));

        for (int i = 0; i < currentString.length(); i++) {
            stringBuilder.append(currentString.charAt(i));
            helper(combinations, phoneMap, digits, currentIndex + 1, stringBuilder);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
    }

    public static void main(String[] args) {
        Test17_2 test17 = new Test17_2();
        System.out.println(test17.letterCombinations("2"));
    }


}
